package io.renren.modules.basedata.service.impl;

import cn.com.taiji.common.manager.net.http.binclient.ApiRequestException;
import cn.com.taiji.sdk.comm.ETCCommHelper;
import cn.com.taiji.sdk.model.comm.protocol.sdts.basics.issuer.AgencyUploadRequest;
import cn.com.taiji.sdk.model.comm.protocol.sdts.basics.issuer.AgencyUploadResponse;
import io.renren.common.utils.CreateFileUtil;
import io.renren.common.utils.DateUtil;
import io.renren.modules.basedata.dao.AgencyUploadDao;
import io.renren.modules.basedata.entity.tbl_agency;
import io.renren.modules.basedata.service.AgencyUploadService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
public class AgencyUploadServiceImpl implements AgencyUploadService {
    private Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    private AgencyUploadDao agencyUploadDao;

    @Override
    public String agencyUpload() throws Exception {
        log.info("开始上传客服合作机构信息");
        List<tbl_agency> list = null;
        tbl_agency agency = null;
        list = getAgencyList();//获取客服合作机构信息
        if(list!=null&&list.size()>0){
            //一条一条上传数据
            for(int i=0;i<list.size();i++){
                agency = list.get(i);
                AgencyUploadRequest request = new AgencyUploadRequest();
                request.setId(agency.getId());
                request.setName(agency.getName());
                request.setTel(agency.getTel());
                request.setContact(agency.getContact());
                request.setStartTime(DateUtil.formatDate(agency.getStartTime()));
                request.setEndTime(DateUtil.formatDate(agency.getEndTime()));
                request.setOperation(agency.getFlag());
                String fileName = ETCCommHelper.getFileName(request,"65");
                //生成json文件
				CreateFileUtil.createJsonFile(request.toJsonStr(), "",fileName);
                try{
                    AgencyUploadResponse response = (AgencyUploadResponse)ETCCommHelper.sdtsUpload(fileName, request, AgencyUploadResponse.class);
                    log.info("上传客服合作机构信息服务器返回:" + response.toJson());
                    if (response != null && response.getInfo().equals("成功！")) {
                        if (agencyUploadDao.updateAgencyUploadflag(agency.getId()) == 1) {
                            log.info("更新客服合作机构信息上传标志成功！");
                        } else {
                            log.info("更新客服合作机构信息上传标志失败！");
                        }

                    } else {
                        log.info("上传客服合作机构信息失败!失败原因:" + response.getInfo());
                        agencyUploadDao.updateAgencyUploadflagError(agency.getId());
                    }
                }catch(IOException e){

                    log.error("上传客服合作机构信息失败！错误信息:",e);
                    e.printStackTrace();
                    continue;
                }catch (ApiRequestException apie) {
                    log.error("上传客服合作机构信息失败！错误信息:",apie);
                    if(apie.getErrCode()>=700&&apie.getErrCode()<=800){
                        if(apie.getErrCode()==707){
                           agencyUploadDao.updateAgencyUploadflag(agency.getId());
                        }else{
                            agencyUploadDao.updateAgencyUploadflagError(agency.getId());                        }
                    }
                    apie.printStackTrace();
                    continue;
                }
            }
            return "上传客服合作机构信息结束！";
        }else{
            return "无客服合作机构信息,上传结束...";
        }
    }

    //获取客服合作机构信息
    @SuppressWarnings("unchecked")
    public List<tbl_agency> getAgencyList() throws IOException {
        List<tbl_agency> list;
        try {
            list = agencyUploadDao.getAgencyList();
        } catch (Exception e) {
            e.printStackTrace();
            throw new IOException("获取客服合作机构信息失败"+e.getMessage());
        }
        return list;
    }
}
